﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Skywave.Reporting
{
    public class HtmlReportFieldRelativeUrl : HtmlReportField
    {
        internal override string GetContent(HtmlReportFieldGetContentArgs e)
        {
            string r = "";
            //
            if (e.Value != null)
            {
                if (e.Value is DataStructure.Pair<string, byte[]>)
                {
                    DataStructure.Pair<string, byte[]> valueTyped = e.Value as DataStructure.Pair<string, byte[]>;
                    if (valueTyped != null && valueTyped.Data2 != null && valueTyped.Data2.Length > 0)
                    {
                        string str1 = valueTyped.Data1;
                        if (!string.IsNullOrEmpty(str1))
                        {
                            if (!e.ContentsDirectory.Exists)
                                e.ContentsDirectory.Create();
                            r = string.Format("FV_{0}_{1}_{2}", e.IndexDataItem, e.IndexField, str1);
                            //
                            System.IO.FileStream fs1 = new System.IO.FileStream(e.ContentsDirectory.FullName + "\\" + r, System.IO.FileMode.Create, System.IO.FileAccess.Write);
                            System.IO.BinaryWriter bw1 = new System.IO.BinaryWriter(fs1);
                            bw1.Write(valueTyped.Data2);
                            bw1.Close();
                            //
                            r = string.Format(".\\{0}\\{1}", e.ContentsDirectory.Name, r);
                        }
                    }
                }
                else if (e.Value is string)
                {
                    string str1 = e.Value.ToString();
                    if (!string.IsNullOrEmpty(str1) && System.IO.File.Exists(str1))
                    {
                        if (!e.ContentsDirectory.Exists)
                            e.ContentsDirectory.Create();
                        r = string.Format("FV_{0}_{1}_{2}", e.IndexDataItem, e.IndexField, System.IO.Path.GetExtension(str1));
                        System.IO.File.Copy(str1, e.ContentsDirectory.FullName + "\\" + r, true);
                        r = string.Format(".\\{0}\\{1}", e.ContentsDirectory.Name, r);
                    }
                }
            }
            //
            return r;
        }

        internal override void Create_Internal(string toParse)
        {
        }
    }
}
